package com.jdb.banner.provider.service.impl;

import com.github.wenhao.jpa.Specifications;
import com.guns21.data.domain.JpaPageResult;
import com.guns21.result.domain.PageResult;
import com.guns21.result.domain.Result;
import com.guns21.support.service.BaseQueryService;
import com.guns21.support.util.WebUtils;
import com.jdb.banner.api.dto.BannerDTO;
import com.jdb.banner.api.query.BannerQuery;
import com.jdb.banner.controller.assembler.BannerAssembler;
import com.jdb.banner.provider.entity.BannerDO;
import com.jdb.banner.provider.repository.BannerRepository;
import com.jdb.banner.provider.service.BannerQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

@Service
public class BannerQueryServiceImpl extends BaseQueryService implements BannerQueryService {

    @Autowired
    private BannerRepository bannerRepository;


    @Override
    public PageResult<BannerDTO> listBanners(Integer count) {
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        Pageable pageable = new PageRequest(0, count, sort);
        Specification<BannerDO> specification = Specifications.<BannerDO>and()
//                .eq(StringUtils.isNotBlank(bannerQuery.getCategoryId()), "categoryId", loanQuery.getCategoryId())
//                .gt(Objects.nonNull(request.getAge()), "age", 18)
//                .between("birthday", new Range<>(new Date(), new Date()))
//                .like("nickName", "%og%", "%me")
                .build();
        Page<BannerDO> all = bannerRepository.findAll(specification, pageable);
        return JpaPageResult.success(all, loanDO -> {
            BannerDTO bannerDTO = BannerAssembler.toDTO(loanDO);
            bannerDTO.setCover(WebUtils.buildWebUrlWithFile(bannerDTO.getCover()));
            return bannerDTO;
        });
    }

    @Override
    public PageResult<BannerDTO> listByPage(BannerQuery bannerQuery) {
        Pageable pageable = new PageRequest(bannerQuery.getPage() - 1, bannerQuery.getPageSize());
        Specification<BannerDO> specification = Specifications.<BannerDO>and()
//                .eq(StringUtils.isNotBlank(bannerQuery.getCategoryId()), "categoryId", loanQuery.getCategoryId())
//                .gt(Objects.nonNull(request.getAge()), "age", 18)
//                .between("birthday", new Range<>(new Date(), new Date()))
//                .like("nickName", "%og%", "%me")
                .build();
        Page<BannerDO> all = bannerRepository.findAll(specification, pageable);
        return JpaPageResult.success(all, loanDO -> BannerAssembler.toDTO(loanDO));
    }

        @Override
    public Result<BannerDTO> get(String id) {
        BannerDO one = bannerRepository.getOne(id);
        return Result.success(BannerAssembler.toDTO(one));
    }
}
